/*********************************************************
 *********************************************************
 ********************                  *******************
 *************                                ************
 *******                  _oo0oo_                  *******
 ***                     o8888888o                     ***
 *                       88" . "88                       *
 *                       (| -_- |)                       *
 *                       0\  =  /0                       *
 *                     ___/`---'\___                     *
 *                   .' \\|     |// '.                   *
 *                  / \\|||  :  |||// \                  *
 *                 / _||||| -:- |||||- \                 *
 *                |   | \\\  -  /// |   |                *
 *                | \_|  ''\---/''  |_/ |                *
 *                \  .-\__  '-'  ___/-. /                *
 *              ___'. .'  /--.--\  `. .'___              *
 *           ."" '<  `.___\_<|>_/___.' >' "".            *
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |          *
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /          *
 *      =====`-.____`.___ \_____/___.-`___.-'=====       *
 *                        `=---='                        *
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      *
 *********__佛祖保佑__永无BUG__验收通过__钞票多多__*********
 *********************************************************/
package org.jbase.jbsc.commons.base.config.handler;

import org.jbase.jbsc.commons.core.result.ResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * Project: jbsc-commons <br/>
 * File: GlobalExceptionHandler.java <br/>
 * Class: org.jbase.jbsc.commons.base.config.handler.GlobalExceptionHandler <br/>
 * Description: <描述类的功能>. <br/>
 * Copyright: Copyright (c) 2011 <br/>
 * Company: https://gitee.com/liuzp315 <br/>
 * Makedate: 2020/9/17 上午10:34 <br/>
 *
 * @author popo
 * @version 1.0
 * @since 1.0
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger L = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 路径错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public ResultBean handlerNoFoundException(Exception e) {
        L.error(e.getMessage(), e);
        return ResultBean.fireFail().setCode("404").setMsg("路径不存在，请检查路径是否正确");
    }

//import org.springframework.security.access.AccessDeniedException;
//import org.springframework.security.authentication.AccountExpiredException;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//    /**
//     * 没有授权
//     *
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(AccessDeniedException.class)
//    public ResultBean handleAuthorizationException(AccessDeniedException e) {
//        L.error(e.getMessage());
//        return ResultBean.fireFail().setCode("403").setMsg("没有权限，请联系管理员授权");
//    }
//
//    /**
//     * 登录超时
//     *
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(AccountExpiredException.class)
//    public ResultBean handleAccountExpiredException(AccountExpiredException e) {
//        L.error(e.getMessage(), e);
//        return ResultBean.fireFail().setCode("50014").setMsg("账号登录超时：" + e.getMessage());
//    }
//
//    /**
//     * 用户名不存在
//     *
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(UsernameNotFoundException.class)
//    public ResultBean handleUsernameNotFoundException(UsernameNotFoundException e) {
//        L.error(e.getMessage(), e);
//        return ResultBean.fireFail().setCode("50008").setMsg("用户名不存在：" + e.getMessage());
//    }

    /**
     * 系统异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResultBean handleException(Exception e) {
        L.error(e.getMessage(), e);
        return ResultBean.fireFail().setMsg("系统异常：：" + e.getMessage());
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public ResultBean validatedBindException(BindException e) {
        L.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return ResultBean.fireFail().setCode("405").setMsg(message);
    }

    /**
     * 方法参数验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultBean validExceptionHandler(MethodArgumentNotValidException e) {
        L.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return ResultBean.fireFail().setCode("405").setMsg(message);
    }
}
